import 'package:mysql1/mysql1.dart';

class ListModel {
  final _settings = ConnectionSettings(
      host: '101.200.210.67',
      port: 3306,
      user: 'root',
      password: 'lcmysql1204',
      db: 'word');

  var conn;

  Future getAll() async {
    conn = await MySqlConnection.connect(_settings);
    var result = await conn.query(
        "select u.user_name, s.time, s.comment from tb_social as s, tb_user as u where u.user_id = s.user_id order by s.time desc");
    await conn.close();
    List<List<String>> res = [];
    for (var row in result) {
      res.add([row[0].toString(), row[1].toString(), row[2].toString()]);
    }
    return res;
  }

  Future insert(String user_id, String time, String comment) async {
    conn = await MySqlConnection.connect(_settings);
    await conn.query(
        'insert into tb_social (user_id, time, comment) values (?, ?, ?)',
        [user_id, time, comment]);
    var result = await conn.query(
        "select u.user_name, s.time, s.comment from tb_social as s, tb_user as u where u.user_id = s.user_id order by s.time desc");
    await conn.close();
    List<List<String>> res = [];
    for (var row in result) {
      res.add([row[0].toString(), row[1].toString(), row[2].toString()]);
    }
    return res;
  }
}
